package com.redpixy.monit.common.util;

/*
 * KDZZgame
 *
 * @(#)StrUtil.java      @(#)Create on :2009-6-28
 * 
 * Copyright (c) 2010 RedPixy, Inc. All rights reserved.
 * This SOURCE CODE FILE, which has been provided by RedPixy Software as part of 
 * a RedPixy Software product for use ONLY by licensed users of the product, includes
 * CONFIDENTIAL and PROPRIETARY information of RedPixy Software.
 * For more information please visit <http://www.redpixy.com/>.
 * 
 */


import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import org.apache.log4j.Logger;

/**
 * @author ZhangLei.
 * @version 2009-6-28
 * 
 * @category 字符串工具类
 * 
 */
public class StrUtil {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(StrUtil.class);

	private static String[] initalStr = new String[] { "0", "1", "2", "3", "4",
			"5", "6", "7", "8", "9", "a", "A", "b", "B", "c", "C", "d", "D",
			"e", "E", "f", "F", "g", "G", "h", "H", "i", "I", "j", "J", "k",
			"K", "l", "L", "m", "M", "n", "N", "o", "O", "p", "P", "q", "Q",
			"r", "R", "s", "S", "t", "T", "u", "U", "v", "V", "w", "W", "x",
			"X", "y", "Y", "z", "Z" };

	private static Random random = new Random();

	private StrUtil() {
	}

	/**
	 * 判断字符串是否为空
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isEmptyStr(String str) {
		if (null == str || "".equals(str.trim())
				|| "null".equals(str.trim().toLowerCase()))
			return true;
		else
			return false;
	}
	/**
	 * 判断字符串是否为空
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isEmptyObj(Object obj) {
		if (null == obj || "".equals(obj.toString())|| "null".equals(obj.toString().toLowerCase()))
			return true;
		else
			return false;
	}

	/**
	 * String Trim
	 * 
	 * @param str
	 * @return
	 */
	public static String formatStr(String str) {
		if (null == str || str.equals("null"))
			return "";
		else

			return str.trim();
	}

	/**
	 * 字符串转换为HTMl
	 * 
	 * @param in
	 * @return
	 */
	public static String textToHtml(String in) {
		StringBuffer out = new StringBuffer();
		for (int i = 0; in != null && i < in.length(); i++) {
			char c = in.charAt(i);
			if (c == '\n')
				out.append("<br>");
			else if (c == ' ')
				out.append("&nbsp;");
			else
				out.append(c);
		}

		return out.toString();
	}

	/**
	 * HTML　to Text
	 * 
	 * @param in
	 * @return
	 */
	public static String htmlToText(String in) {
		String TextStr = "";
		int i = 0, pos = 0;

		for (i = 0; in != null && i < in.length() - 4;) {
			pos = in.indexOf("<br>", i);
			if (pos != -1) {
				TextStr = TextStr + in.substring(i, pos) + "\n";
				i = pos + 4;
			} else {
				TextStr = TextStr + in.substring(i);
				break;
			}
		}

		return TextStr;
	}

	/**
	 * 返回字符串的Uinicode码
	 * 
	 * @param inStr
	 * @return
	 */
	public static String getUnicodeStr(String inStr) {
		StringBuffer unicode = new StringBuffer();
		char c;
		int bit;
		String tmp = null;

		for (int i = 0; i < inStr.length(); i++) {
			c = inStr.charAt(i);
			if (c > 255) {
				unicode.append("\\u");

				bit = (c >>> 8);
				tmp = Integer.toHexString(bit);
				if (tmp.length() == 1)
					unicode.append("0");

				unicode.append(tmp);
				// debug = "\\u" + tmp;

				bit = (c & 0xFF);
				tmp = Integer.toHexString(bit);

				if (tmp.length() == 1)
					unicode.append("0");

				unicode.append(tmp);
				// debug+= tmp;
			} else {
				unicode.append(c);
				// debug = c + "";
			}
			// logger.info(c + "------>:" + debug);
		}

		// logger.info(inStr + "------>:" + unicode.toString());
		return (new String(unicode));
	}

	/**
	 * 将字符串 MD5 加密
	 * 
	 * @param info
	 * @return
	 * @throws NoSuchAlgorithmException
	 */
	public static String encryptInfoWithMD5(String info)
			throws NoSuchAlgorithmException {

		MessageDigest md5MD = MessageDigest.getInstance("MD5");

		md5MD.update(info.getBytes());
		// md5MD.update(info.getBytes("UTF-8"));
		return getHexEncode(md5MD.digest());
	}

	/**
	 * 返回字节数组的哈希码
	 * 
	 * @param bytes
	 * @return
	 */
	public static String getHexEncode(byte bytes[]) {
		StringBuffer buf = new StringBuffer(bytes.length * 2);

		for (int i = 0; i < bytes.length; i++) {
			if ((bytes[i] & 0xff) < 16)
				buf.append("0");

			buf.append(Long.toString(bytes[i] & 0xff, 16));
		}

		return buf.toString();
	}

	/**
	 * 将对象数组字符话
	 * 
	 * @param array
	 * @return
	 */
	public static String arrayToSqlStr(Object array) {
		Object[] parameter = null;

		String delimiter = "";
		if (array instanceof Integer[]) {
			parameter = (Integer[]) array;
			// logger.debug("int");
		}

		if (array instanceof Long[]) {
			parameter = (Long[]) array;
			// logger.debug("long");
		}

		if (array instanceof String[]) {
			delimiter = "'";
			parameter = (String[]) array;
			// logger.debug("String");
		}

		StringBuffer sqlStr = new StringBuffer();
		for (int i = 0; i < parameter.length; i++)
			sqlStr.append(delimiter).append(
					(Object) parameter[i].toString().trim()).append(delimiter)
					.append(",");

		sqlStr.deleteCharAt(sqlStr.length() - 1);

		return sqlStr.toString();
	}

	/**
	 * 返回一个随即字符串,长度 i.
	 * 
	 * @param i
	 * @return
	 */
	public static String randomString(int i) {
		synchronized (StrUtil.class) {
			StringBuffer buffer = new StringBuffer();
			while (i > 0) {
				buffer.append(initalStr[random.nextInt(initalStr.length)]);
				i--;
			}
			return buffer.toString();
		}
	}
	/**
	 * 判断一个字符串是不是数字组成
	 * @param s 字符。
	 * @return
	 */
	public static boolean isDigits(String s){
		if(s==null||s.length()==0)return false;
		for(int i=0;i<s.length();i++){
			if(!Character.isDigit(s.charAt(i)))return false;
		}
		return true;
	}
	/**
	 *将字#分别各符串为MAP 
	 * @param s 字符。
	 * @return
	 */
	public static Map<Integer,Double> StringTOMap(String s){
		Map<Integer, Double> map = new HashMap<Integer, Double>();
		String[] params = s.split("#");
		for (String value : params) {
			Integer key = Integer.valueOf(value.split("=")[0]);
			Double val = Double.valueOf(value.split("=")[1]);
			map.put(key, val);
		}
		return map;
	}
}
